package org.hit.burkun.stat;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.hit.burkun.db.DBHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * @author mingchen
 * @date 2015年8月14日
 */
public class UserIndex {
	private final Logger log = LoggerFactory.getLogger(UserIndex.class);
	// parameters
	private DBHelper dbh;
	private Map<String, Double> userAct = null;
	private Map<String, Double> userFav = null;
	private double [] groupWeight;
	private double [] favoriteWeight;

	/**
	 * 
	 * Constructor function of Class
	 */
	public UserIndex() {
		userAct = new HashMap<String, Double>();
		userFav = new HashMap<String,Double>();
		init();
	}

	public UserIndex(double[] groupWeight, double[] favoriteWeight) {
		this.groupWeight = groupWeight;
		this.favoriteWeight = favoriteWeight;
		userAct = new HashMap<String, Double>();
		userFav = new HashMap<String, Double>();
		init();
	}

	/**
	 * 
	 * Initialize the DB handler void 2015年8月10日
	 */
	public void init() {
		dbh = DBHelper.getServerDataDBHelper();
		dbh.connectionDB();
	}

	/**
	 * calculate the active of the user
	 * 
	 * void 2015年8月14日
	 */
	public void getActInfo() {
		String sql = "select uid,mid_count,days from train_user_stat;";
		Collection<String[]> res = dbh.queryData(sql, 3);
		for (String[] line : res) {
			double value = Integer.parseInt(line[1]) / Integer.parseInt(line[2]);
			// uid:active
			log.info(line[0] + " dumping");
			userAct.put(line[0], Double.valueOf(judgeActiveLevel(value)));
		}
	}

	/**
	 * dump the user active info local
	 * 
	 * @param obj
	 * @param fileName
	 *            void
	 * 
	 *            2015年8月14日
	 */
	public void serializeObj(Map<String, Double> obj, String fileName) {
		Serialization.save(obj, fileName);
		log.info("Serialization done!" + fileName);
	}

	/**
	 * 
	 * @param value
	 * @return double 2015年8月14日
	 */
	public double judgeActiveLevel(double value) {
		if (value >= 5.0) {
			return groupWeight[0];
		} else if (value >= 3) {
			return groupWeight[1];
		} else if (value >= 1) {
			return groupWeight[2];
		}
		return 0.0;
	}

	/**
	 * Calculate the user favorite information
	 * 
	 * void 2015年8月14日
	 */
	public void getFavoriteInfo() {
		if (userAct.size() == 0) {
			getActInfo();
		}
		// calculate the user favorite information
		for(String uid:userAct.keySet()){
			String sql = "select repost,comment,likes from sina_train where uid = \""+uid+"\";";
			Collection<String[]> res = dbh.queryData(sql, 3);
			double comment = 0.0, repost = 0.0, likes = 0.0;
			int c_count = 0, r_count = 0, l_count = 0;
			for (String[] lines : res) {
				repost += Double.parseDouble(lines[0]) * favoriteWeight[0];
				r_count += Double.parseDouble(lines[0]);
				comment += Double.parseDouble(lines[1]) * favoriteWeight[1];
				c_count += Double.parseDouble(lines[1]);
				likes += Double.parseDouble(lines[2]) * favoriteWeight[2];
				l_count += Double.parseDouble(lines[2]);
			}
			log.info(uid + ":" + r_count + "," + c_count + "," + l_count);
			userFav.put(uid, Double.valueOf(repost/r_count + comment / c_count + likes / l_count));
		}
		log.info("Get Favorite Information Done!");

	}

	/**
	 * dump user info
	 * 
	 * void 2015年8月14日
	 */
	public void dump() {
		serializeObj(userAct, "userAct.info");
		serializeObj(userFav, "userFav.info");
	}

}
